package com.trainoo.nio.file;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p> NIO写入文件 </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2020/6/18 16:37
 */
@Slf4j
public class WriterFile {


    @Data
    static class TestBo {
        private String v1;
        private Integer v2;
        private Long v3;
        private Date date;
        TestBo(){}
        TestBo(String v1, Integer v2, Long v3, Date date){
            this.v1 = v1;
            this.v2 = v2;
            this.v3 = v3;
            this.date = date;
        }
    }

    private static List<TestBo> boList = new ArrayList<>();

    static {
        for (int i = 0; i < 1000; i++) {
            boList.add(new TestBo("aaaa", 100, 223079087L, new Date()));
            boList.add(new TestBo("bbbb", 101, 323079087L, new Date()));
            boList.add(new TestBo("cccc", 102, 423079087L, new Date()));
            boList.add(new TestBo("dddd", 103, 523079087L, new Date()));
            boList.add(new TestBo("eeee", 104, 623079087L, new Date()));
            boList.add(new TestBo("ffff", 105, 723079087L, new Date()));
            boList.add(new TestBo("gggg", 106, 823079087L, new Date()));
            boList.add(new TestBo("hhhh", 107, 923079087L, new Date()));
            boList.add(new TestBo("iiii", 108, 923079087L, new Date()));
            boList.add(new TestBo("jjjj", 109, 923079087L, new Date()));
        }
    }


    public static void main(String[] args) throws Exception {

        // 指定文件写入类型append为true, 追加文件内容
        try (PrintStream ps = new PrintStream(new FileOutputStream(new File("test.txt"), true))){
            // 写入文件
            boList.forEach(record -> {
                StringBuilder sb = new StringBuilder().append(record.getV1()).append(",")
                        .append(record.getV2()).append(",")
                        .append(record.getV3()).append(",")
                        .append(record.getDate());
                log.info("输出行>>> " + sb.toString());
                ps.println(sb.toString());
            });
        }
    }
}
